# !/usr/bin/python
# -*- coding: utf-8 -*-

# THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW: 
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),  
# to deal in the Software without restriction, including without limitation  
# the rights to use, copy, modify, merge, publish, distribute, sublicense,  
# and/or sell copies of the Software, and to permit persons to whom the  
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in 
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
# DEALINGS IN THE SOFTWARE.

# object.py
# written by: 
#     João Grilo <joao.grilo@gmail.com>
#     Jan Jokela <jan@devion.org>

__version__ = 0.3
__doc__ = 'Media objects'

import mmpython
import engines.metadata.parser

class Audio:
    """The class that represents all kinds of audio objects.

    To be noted, is the attributes present, which represent the
    lowest common denominator found in most files of this type
    (like mp3, ogg/vorbis, wma, etc.).
    """

    def __init__(self, uri, length=0, size=0,
                 media_type='audio', mime_type='', track_number=0,
                 title='', artist='', album='', genre='', year=0, album_art=''):
        self.uri = uri
        self.length = length    # The length in seconds
        self.size = size       # The size in bytes
        self.media_type = media_type
        self.mime_type = mime_type
        self.track_number = track_number
        self.title = title
        self.artist = artist
        self.album = album
        self.genre = genre
        self.year = year
        self.album_art = album_art

    def update_metadata(self, metadata=None, metadata_web=None):
        """Updates the metadata concerning this object.

        This procedure is not done automatically to prevent excessive
        delays when performing massive operations. Imagine creating
        a playlist for all the musics of genre "classical". The user
        might have several gigabytes of it, and gathering the metadata
        at the same time it tries to create objects from this class and
        sort them all out, would be too high for common use.

        We allow the user to pass the "metadata" object, in case it
        has already done so to find, for example, the mimetype. This
        will avoid the overhead of calling the method twice.

        Keyword arguments:
        metadata (mmpython.parse()) -- The metadata to populate.

        """
        if metadata == None:
            metadata = mmpython.parse(self.uri)
        if metadata_web == None:
            metadata_web = engines.metadata.parser.Generic()
        try:
            self.length = metadata.length
            self.size = metadata.size
            self.mime_type = metadata.mime
            self.track_number = metadata.trackno
            self.title = metadata.title
            self.artist = metadata.artist
            self.album = metadata.album
            self.genre = metadata.genre
            self.year = metadata.date
            
            metadata_web.parse(self.title, self.album, self.artist, self.length)
            
            self.album = metadata_web.get_album()
            self.artist = metadata_web.get_artist()
            self.album_art = metadata_web.get_album_art()
            
            if self.title == '' or self.title == 'None' or self.title == 'Unknown' or self.title == '':
                self.title = None
            if self.artist == '' or self.artist == 'None' or self.artist == 'Unknown' or self.artist == '':
                self.artist = None
            if self.album == '' or self.album == 'None' or self.album == 'Unknown' or self.album == '':
                self.album = None
            if self.genre == '' or self.genre == 'None' or self.genre == 'Unknown' or self.genre == '':
                self.genre = None
        except:
            pass


class Video:

    def __init__(self, uri, length=0, size=0, 
                media_type='video', mime_type='', title=''):
        self.uri = uri
        self.length = length    # The length in seconds
        self.size = size       # The size in bytes
        self.media_type = media_type
        self.title = title
        self.mime_type = mime_type

    def update_metadata(self, metadata=None):
        """Updates the metadata concerning this object.

        This procedure is not done automatically to prevent excessive
        delays when performing massive operations. Imagine creating
        a playlist for all the musics of genre "classical". The user
        might have several gigabytes of it, and gathering the metadata
        at the same time it tries to create objects from this class and
        sort them all out, would be too high for common use.

        We allow the user to pass the "metadata" object, in case it
        has already done so to find, for example, the mimetype. This
        will avoid the overhead of calling the method twice.

        Keyword arguments:
        metadata (mmpython.parse()) -- The metadata to populate.

        """
        if metadata == None:
            metadata = mmpython.parse(self.uri)
        self.length = metadata.length
        self.size = metadata.size
        self.title = metadata.title
        self.mime_type = metadata.mime

class Image:

    def __init__(self, uri, length=0, size=0, 
                media_type='image', mime_type='', title='',
                width=0, height=0):
        self.uri = uri
        self.size = size       # The size in bytes
        self.media_type = media_type
        self.title = title
        self.mime_type = mime_type
        self.width = width
        self.height = height

    def update_metadata(self, metadata=None):
        """Updates the metadata concerning this object.

        This procedure is not done automatically to prevent excessive
        delays when performing massive operations. Imagine creating
        a playlist for all the musics of genre "classical". The user
        might have several gigabytes of it, and gathering the metadata
        at the same time it tries to create objects from this class and
        sort them all out, would be too high for common use.

        We allow the user to pass the "metadata" object, in case it
        has already done so to find, for example, the mimetype. This
        will avoid the overhead of calling the method twice.

        Keyword arguments:
        metadata (mmpython.parse()) -- The metadata to populate.

        """
        if metadata == None:
            metadata = mmpython.parse(self.uri)
        self.size = metadata.size
        self.title = metadata.title
        self.mime_type = metadata.mime
        self.width = metadata.width
        self.height = metadata.height
